package org.amphiprion.descentv2assistant;

import org.amphiprion.descentv2assistant.adapter.GameSessionViewPagerAdapter;
import org.amphiprion.descentv2assistant.ui.fragment.EncounterHeroListFragment;
import org.amphiprion.descentv2assistant.ui.fragment.EncounterQuestListFragment;
import org.amphiprion.descentv2assistant.ui.views.TabPageIndicator;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;

public class GameSessionActivity extends SherlockFragmentActivity {
	public final static int REQUEST_CODE_TAKE_PHOTO_HERO = 1;
	public final static int REQUEST_CODE_TAKE_PHOTO_QUEST = 2;
	public final static int REQUEST_CODE_TAKE_PHOTO_OVERLORD = 3;

	private ViewPager mPager;
	private GameSessionViewPagerAdapter mPagerAdapter;
	private TabPageIndicator mTabIndicator;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_game_session);

		mPager = (ViewPager) findViewById(R.id.viewPager);
		mTabIndicator = (TabPageIndicator) findViewById(R.id.indicator);

		mPagerAdapter = new GameSessionViewPagerAdapter(this, getSupportFragmentManager());
		mPager.setAdapter(mPagerAdapter);

		mTabIndicator.setViewPager(mPager);

		// Action bar
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		getSupportActionBar().setDisplayShowHomeEnabled(false);
		getSupportActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_support_bar));

		setTitle(App.currentGameSession.getName());
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			onBackPressed();
			break;
		case R.id.menu_add_monster:
			mPagerAdapter.addMonster(mPager.getCurrentItem());
			break;
		}
		return true;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getSupportMenuInflater();
		inflater.inflate(R.menu.game_session, menu);
		return true;
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == Activity.RESULT_OK) {
			if (requestCode == REQUEST_CODE_TAKE_PHOTO_HERO) {
				EncounterHeroListFragment.sThis.photoTaken(data);
			} else if (requestCode == REQUEST_CODE_TAKE_PHOTO_QUEST) {
				EncounterQuestListFragment.sThis.photoQuestTaken(data);
			} else if (requestCode == REQUEST_CODE_TAKE_PHOTO_OVERLORD) {
				EncounterQuestListFragment.sThis.photoOverlordTaken(data);
			}
		}
	}
}
